package com.qiantu.phone.ui.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.qiantu.api.entity.LinkageBean;
import com.qiantu.api.entity.LinkageStartStop;
import com.qiantu.phone.R;
import com.qiantu.phone.app.AppAdapter;
import com.qiantu.phone.app.AppApplication;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

public final class SetLinkageAdapter extends AppAdapter<LinkageBean> {

    private boolean isSet;
    private int type;
    private LinkedHashMap<String, LinkageBean> selected = new LinkedHashMap();
    public SetLinkageAdapter(Context context, boolean isSet, int type) {
        super(context);
        this.isSet = isSet;
        this.type = type;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ViewHolder();
    }

    private final class ViewHolder extends AppAdapter<?>.ViewHolder {

        private final TextView tvName;
        private final TextView tvStartStop;
        private final TextView checkView;

        private ViewHolder() {
            super(R.layout.item_select_linkage);
            tvName = findViewById(R.id.tv_name);
            tvStartStop = findViewById(R.id.tv_start_stop);
            checkView = findViewById(R.id.check_view);
        }

        @Override
        public void onClick(View view) {
//            checkView.setSelected(!checkView.isSelected());
            LinkageBean data = (LinkageBean) itemView.getTag();
//            if(!checkView.isSelected()){
//                if(type == AddTaskActivity.TYPE_LINKAGE){
//                    selected.put(data.getLinkageSerialNo(), data);
//                }else if(type == AddTaskActivity.TYPE_SCENE){
//                    selected.put(data.getLinkageSerialNo(), data);
//                }
//            }else{
//                if(type == AddTaskActivity.TYPE_LINKAGE){
//                    selected.remove(data.getLinkageSerialNo());
//                }else if(type == AddTaskActivity.TYPE_SCENE){
//                    selected.remove(data.getLinkageSerialNo());
//                }
//            }
//            notifyDataSetChanged();
            super.onClick(view);
        }

        @Override
        public void onBindView(int position) {
            LinkageBean linkage = getItem(position);
            tvName.setText(linkage.getName());
            itemView.setTag(getItem(position));
            if(isSet){
                checkView.setSelected(true);
                if(linkage.isIsDisable()){
                    tvStartStop.setText(getString(R.string.stop_using));
                    tvStartStop.setTextColor(AppApplication.getInstance().getThemeColor(R.attr.textColor3));
                }else{
                    tvStartStop.setText(getString(R.string.start_using));
                    tvStartStop.setTextColor(AppApplication.getInstance().getThemeColor(R.attr.textColor2));
                }
            }else{
                RecyclerView.LayoutParams param = (RecyclerView.LayoutParams)itemView.getLayoutParams();
                LinkageBean selectedLinkageBean = null;
                selectedLinkageBean = selected.get(linkage.getLinkageSerialNo());
                if (selectedLinkageBean != null){
                    itemView.setVisibility(View.GONE);
                    param.height = 0;
                    param.width = 0;
                    checkView.setSelected(true);
                }else{
                    //未设置的联动全部默认成启用,设置后可以修改停启用
                    linkage.setIsDisable(false);
                    param.width = RecyclerView.LayoutParams.MATCH_PARENT;
                    param.height = RecyclerView.LayoutParams.WRAP_CONTENT;
                    itemView.setVisibility(View.VISIBLE);
                    checkView.setSelected(false);
                }
                itemView.setLayoutParams(param);
                tvStartStop.setVisibility(View.GONE);
            }
        }
    }

    public void removeSelectedBySerialNo(String linkageSerialNo) {
        selected.remove(linkageSerialNo);
        notifyDataSetChanged();
    }

    public void putSelected(LinkageBean linkage){
        selected.put(linkage.getLinkageSerialNo(), linkage);
        notifyDataSetChanged();
    }

    public List<LinkageBean> getSelectedData() {
        List<LinkageBean> checkList = new ArrayList<>();
        for(String key : selected.keySet()){
            checkList.add(selected.get(key));
        }
        return checkList;
    }

    //场景任务的联动通过SerialNo比对
    public void checkSelectedBySerialNo(List<LinkageStartStop> linkageStartStops){
        selected.clear();
        if(linkageStartStops != null && getData() != null && getData().size() > 0){
            for(LinkageStartStop linkageStartStop : linkageStartStops){
                for(LinkageBean linkageBean : getData()){
                    if(linkageBean.getLinkageSerialNo().equals(linkageStartStop.getLinkageSerialNo())){
                        linkageBean.setIsDisable(linkageStartStop.isDisable());
                        selected.put(linkageBean.getLinkageSerialNo(), linkageBean);
                        break;
                    }
                }
            }
            notifyDataSetChanged();
        }
    }
}